package com.woniu.goods.controller;


import com.github.pagehelper.PageInfo;
import com.woniu.goods.entity.Goods;
import com.woniu.goods.entity.Order;
import com.woniu.goods.service.GoodsServiceImpl;
import com.woniu.goods.service.OrderServiceImpl;
import com.woniu.goods.vo.OrderVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    Logger logger=LoggerFactory.getLogger(UserController.class);

    @Autowired
    GoodsServiceImpl goodsService;

    @Autowired
    OrderServiceImpl orderService;

        @RequestMapping("/login")
    public String login(String username, String password, HttpServletRequest request, Map map){
        //获取当前的用户
        Subject subject= SecurityUtils.getSubject();
        if (!subject.isAuthenticated()){
            UsernamePasswordToken token=new UsernamePasswordToken(username,password);
            try {
                subject.login(token);
                logger.info("token.hashCode() +\"--->Controller中的token\"");
            }catch (UnknownAccountException e) {
                map.put("errorInfo","账户不存在!");
                logger.info("当前账户不存在--->"+username);
                return "login" ;
            }catch (IncorrectCredentialsException e) {
                map.put("errorInfo","密码错误!");
                logger.info("当前密码不正确--->"+password);
                return "login" ;
            }catch (LockedAccountException e) {
                map.put("errorInfo","账户被锁定!!");
                logger.info("当前账户被锁定--->"+username);
                return "login" ;
            }catch (AuthenticationException e){
                map.put("errorInfo","未知错误！!!");
                logger.info("未知错误--->"+username);
                return "login" ;
            }
        }
        return "redirect:list";
        }


     @RequestMapping("/list")
    public String goodsList(String gname, Integer typeId,
                            @RequestParam(value = "now",defaultValue = "1")Integer now,
                            @RequestParam(value = "size",defaultValue = "3")Integer size,
                            Map map){
        PageInfo<Goods>pageInfo=goodsService.queryGoodsByPage(gname,typeId,now,size);
        map.put("pageInfo",pageInfo);
        map.put("ganem",gname);
        map.put("typeId",typeId);
        return "main";
    }


    @RequestMapping("/orderList")
    public String orderList(HttpServletRequest request,
                            @RequestParam(value = "now",defaultValue = "1")Integer now,
                            @RequestParam(value = "size",defaultValue = "3")Integer size,
                            Map map){
        Integer userid= (Integer) request.getSession().getAttribute("userid");
        PageInfo<OrderVo>pageInfo=orderService.queryOrderByPage(userid,now,size);
        map.put("pageInfo",pageInfo);
        System.out.println("pageInfo123"+pageInfo.getList());
        map.put("userid",userid);
       return "order";
    }
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public String deleteOrder(Integer id){
        int order=orderService.deleteOrder(id);
        return "1";
    }

}
